<?php

namespace App\Admin\Controllers;

use App\Model\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use function foo\func;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Model\User';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->column('user_id', __('User id'));
        $grid->column('phone', __('Phone'));
        $grid->column('username', __('Username'));
        $grid->column('user_info.id_card', __('Id_card'));
        $grid->column('user_info.wx_card', __('Wx_card'));
        $grid->column('face', __('Face'))->display(function ($face){
            return $face??'';
        })->image('',60);
        $grid->column('lat', __('Lat'));
        $grid->column('lon', __('Lon'));
        $grid->column('area_id', __('Area id'));
        $grid->column('deleted_at', __('Deleted at'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('user_id', __('User id'));
        $show->field('phone', __('Phone'));
        $show->field('username', __('Username'));
        $show->field('face', __('Face'))->image();
        $show->field('album', __('Album'))->carousel();
        $show->field('introduction', __('Introduction'))->unescape();
        $show->field('lat', __('Lat'));
        $show->field('lon', __('Lon'));
        $show->field('area_id', __('Area id'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->user_info('用户详情',function ($show){
            $show->field('id_card',__('Id_card'));
            $show->field('wx_card',__('Wx_card'));
        });
        $show->locations('地址',function ($grid){
            $grid->column('address',__('Address'));
        });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->mobile('phone', __('Phone'))
            ->creationRules(['required', 'regex:/^1[345789][0-9]{9}$/', "unique:users"])
            ->updateRules(['required', 'regex:/^1[345789][0-9]{9}$/', "unique:users,phone,{{id}},user_id"]);
        $form->password('password', __('Password'));
        $form->text('username', __('Username'))->rules(
            ['required', 'regex:/^[\x{4e00}-\x{9fa5}]{2,4}$/u'],
            [
                'regex' => '2-4位中文姓名',
            ]
        );
        $form->text('user_info.id_card',__('Id_card'));
        $form->text('user_info.wx_card',__('Wx_card'));
        $form->image('face', __('Face'));
        $form->multipleImage('album', __('Album'))->removable()->sortable();
        $form->editor('introduction', __('Introduction'));
        $form->latlong('lat', 'lon', 'Position');
        $form->number('area_id', __('Area id'));
        $form->hasMany('locations','地址',function (Form\NestedForm $form){
            $form->text('address',__('Address'));
        });
        $form->saving(function (Form $form) {
            if (!trim($form->password)) {
                if ($form->model()->password) {
                    $form->password = $form->model()->password;
                } else {
                    $form->password = md5(123456);
                }
            } else {
                $form->password = md5($form->password);
            }
        });
        return $form;
    }
}
